package com.xxx.lib.base

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.xxx.lib.R
import org.greenrobot.eventbus.EventBus

/**
 * Fragment一级基类
 */
abstract class BaseFragment : Fragment(), IBaseView {

    /**
     * 初始化布局
     */
    abstract fun initContentView(): View?

    /**
     * 初始化操作
     */
    abstract fun init()

    /**
     * 是否初始化
     */
    private var isInited = true

    /**
     * 通用加载框
     */
    private val alertDialog: AlertDialog by lazy {
        AlertDialog.Builder(requireContext()).setView(R.layout.dialog_loading).create().apply {
            setCanceledOnTouchOutside(false)
            window?.setBackgroundDrawable(ColorDrawable())
            window?.setDimAmount(0F)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        return initContentView()
    }

    override fun onResume() {
        super.onResume()
        if (isInited) {
            isInited = false
            init()
        }
    }

    /**
     * 显示加载框
     * @param hint 提示语，可不传
     */
    override fun showLoadingDialog(hint: String) {
        if (activity?.isFinishing != false) return
        if (activity is BaseActivity) {
            (activity as? BaseActivity)?.showLoadingDialog(hint)
        } else {
            activity?.runOnUiThread {
                try {
                    if (!alertDialog.isShowing) alertDialog.show()
                    setLoadingDialogHint(hint)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }

    /**
     * 隐藏加载框
     */
    override fun dismissLoadingDialog() {
        if (activity?.isFinishing != false) return
        if (activity is BaseActivity) {
            (activity as? BaseActivity)?.dismissLoadingDialog()
        } else {
            activity?.runOnUiThread {
                try {
                    alertDialog.dismiss()
                    setLoadingDialogHint("")
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }

    /**
     * 设置加载框提示
     * @param hint 提示语
     */
    private fun setLoadingDialogHint(hint: String) {
        if (activity?.isFinishing != false) return
        if (activity is BaseActivity) {
            (activity as BaseActivity).showLoadingDialog(hint)
        } else {
            activity?.runOnUiThread {
                try {
                    alertDialog.findViewById<TextView>(R.id.tvHint)?.apply {
                        visibility = if (hint.isBlank()) View.GONE else View.VISIBLE
                        text = hint
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }

    /**
     * 注册EventBus
     */
    protected fun registerEventBus() {
        EventBus.getDefault().run {
            if (!isRegistered(this@BaseFragment)) register(this@BaseFragment)
        }
    }

    /**
     * 注销EventBus
     */
    private fun unregisterEventBus() {
        EventBus.getDefault().run {
            if (isRegistered(this@BaseFragment)) unregister(this@BaseFragment)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus()
        dismissLoadingDialog()
    }

}